package cn.wps.moffice.main.local.filebrowser.operator.seek;

import android.text.TextUtils;

import java.io.File;
import java.util.HashMap;

import cn.wps.moffice.OfficeApp;

/**
 * 黑白名单
 */
public class BWList
{
	public  static final int TYPE_WHITE = 0; //白名单 搜索优先
	public static final int TYPE_BLACK = 1;  //黑名单直接跳过
	public static final int TYPE_GARY = 2; //灰名单 搜索最后
	
	/**
	 * tencent
	 */
	private static final String TENCENT_QQFILERECV = "tencent/QQfile_recv";
	private static final String TENCENT_QQLITEFILE= "tencent/QQLitefile_recv";
	private static final String TENCENT_MICROMSG_DOWNLOAD = "tencent/MicroMsg/Download";
	private static final String TENCENT_QQMAIL_ATTACHMENT = "tencent/QQmail/attachment";
	
	/**
	 * yixin
	 */
	private static final String YIXIN_MAIL = "Yixin/mail";
	private static final String YIXIN_FILE = "Yixin/file";
	
	/**
	 * 浏览器
	 */
	private static final String UCDOWNLOADS = "UCDownloads";
	private static final String UCDownloadsHD = "UCDownloadsHD";
	private static final String QQBROWSER_DOCUMENT = "QQBrowser/文档";
	private static final String MXBROWSER_DOWNLOADS = "MxBrowser/Downloads";
	private static final String KBROWSER_FAST_DPWMLOADS = "kbrower_fast/download";
	
	/**
	 * mail
	 */
	private static final String TENCTENT_CACHE_ATTACHMENT = "Android/data/com.tencent.androidqqmail/cache/attachment";
	private static final String YAHOO_MAIL_ATTACHMENTS = "yahoo mail/attachments";
	private static final String MY_MAIL_ATTACHMENTS = "Android/data/com.my.mail/cache/attachments";
	private static final String NETEASE_163MAIL = "Netease/163mail";
	private static final String NETEASE_MAIL = "Netease/Mail";
	
	/**
	 * 云存储
	 */
	private static final String WEIYUN = "微云保存的文件";
	private static final String DROPDOX = "Android/data/com.dropbox.android/files";
	private static final String GOOGLEDOCS = "Android/data/com.google.android.apps.docs/files";
	private static final String EVERNOTE = "Android/data/com.google.android.apps.docs/files";
	private static final String BAIDUNETDISK = "BaiduNetdisk";
	private static final String KUAIPAN = "我的快盘";
	
	/**
	 * 办公
	 */
	private static final String ADOBE_READER = "Android/data/com.adobe.reader/files";
	private static final String MSO_OFFICE = "Android/data/com.microsoft.office.officehub/files";
	private static final String GOOGLE_SHEETS = "Android/data/com.google.android.apps.docs.editors.sheets/files";
	private static final String GOOGLE_SLIDES = "Android/data/com.google.android.apps.docs.editors.slides/files";
	private static final String INFRAWARE = "Android/data/com.infraware.office.link/files";
	private static final String MOBISYSTEMS = "Android/data/com.mobisystems.office/files";
	private static final String GOOGLE_DOCS = "Android/data/com.google.android.apps.docs.editors.docs/files";
	
	/**
	 * 下载
	 */
	private static final String DOWNLOAD = "download";
	
	/**
	 * 其它
	 */
	private static final String DUOKAN = "DuoKan/Downloads";
	private static final String XUNLEI = "Android/obb/com.xunlei.downloadprovider";
	private static final String IREADER = "iReader";
	private static final String ZAPYA = "zapya/misc";
	private static final String KINDLE = "Android/data/com.amazon.kindle/files";
	private static final String UTORRENT = "Android/data/com.utorrent.client/files/Download";
	private static final String BEAM = "beam";

	/**
	 * 灰名单
	 */
	private static final String GARY_QZONE_CACHE = "Android/data/com.qzone/cache/image";		
	
	/**
	 * 黑名单
	 */
	private static final String BLACK_MICROMSG = "tencent/MicroMsg";
	private static final String BLACK_MOBILEQQ = "tencent/MobileQQ";
	private static final String BLACK_MOBILEQQ1 = "tencent/msflogs/com/tencent/mobileqq";
	private static final String BLACK_AUTONAVI = "autonavi";
	private static final String BLACK_AUTONAVIDATA50 = "autonavidata50";
	private static final String BLACK_MIPUSH = "mipush";
	private static final String BLACK_TENCENTNEWS = "Android/data/com.tencent.news";
	private static final String BLACK_ZHIHU= "Android/data/com.zhihu.android";	
	private static final String BLACK_BAIDUMAP = "BaiduMap";

	private static final String BLACK_BAIDUMAP_EXT = "BaiduMap";
	private static final String BLACK_AUTONAVIDATA50_EXT = "autonavidata50";	
	private static final String BLACK_ZHIHU_EXT= "Android/data/com.zhihu.daily.android"; 

	
	private static final String EXT_SDCARD = "/storage/extSdCard/";
	
	/**
	 * 表示子目录的状态
	 */
	private static HashMap<String, Integer> children = null;
		
	/**
	 * 初始化黑白名单信息
	 */
	public static HashMap<String, Integer> getBwMap() {
		if (children == null) {
			children = new HashMap<String, Integer>();
			String sdcard = OfficeApp.getInstance().sdcardPath + File.separator;

			// ===白名单====
			// tencent
			initList(sdcard, TENCENT_MICROMSG_DOWNLOAD, TYPE_WHITE);
			initList(sdcard, TENCENT_QQFILERECV, TYPE_WHITE);
			initList(sdcard, TENCENT_QQLITEFILE, TYPE_WHITE);
			initList(sdcard, TENCENT_QQMAIL_ATTACHMENT, TYPE_WHITE);

			// yixin
			initList(sdcard, YIXIN_FILE, TYPE_WHITE);
			initList(sdcard, YIXIN_MAIL, TYPE_WHITE);

			// 浏览器
			initList(sdcard, UCDOWNLOADS, TYPE_WHITE);
			initList(sdcard, UCDownloadsHD, TYPE_WHITE);
			initList(sdcard, QQBROWSER_DOCUMENT, TYPE_WHITE);
			initList(sdcard, MXBROWSER_DOWNLOADS, TYPE_WHITE);
			initList(sdcard, KBROWSER_FAST_DPWMLOADS, TYPE_WHITE);

			// mail
			initList(sdcard, TENCTENT_CACHE_ATTACHMENT, TYPE_WHITE);
			initList(sdcard, YAHOO_MAIL_ATTACHMENTS, TYPE_WHITE);
			initList(sdcard, MY_MAIL_ATTACHMENTS, TYPE_WHITE);
			initList(sdcard, NETEASE_163MAIL, TYPE_WHITE);
			initList(sdcard, NETEASE_MAIL, TYPE_WHITE);

			// 云存储
			initList(sdcard, WEIYUN, TYPE_WHITE);
			initList(sdcard, DROPDOX, TYPE_WHITE);
			initList(sdcard, GOOGLEDOCS, TYPE_WHITE);
			initList(sdcard, EVERNOTE, TYPE_WHITE);
			initList(sdcard, BAIDUNETDISK, TYPE_WHITE);
			initList(sdcard, KUAIPAN, TYPE_WHITE);

			// 办公
			initList(sdcard, ADOBE_READER, TYPE_WHITE);
			initList(sdcard, MSO_OFFICE, TYPE_WHITE);
			initList(sdcard, GOOGLE_SHEETS, TYPE_WHITE);
			initList(sdcard, GOOGLE_SLIDES, TYPE_WHITE);
			initList(sdcard, INFRAWARE, TYPE_WHITE);
			initList(sdcard, MOBISYSTEMS, TYPE_WHITE);
			initList(sdcard, GOOGLE_DOCS, TYPE_WHITE);

			// 下载
			initList(sdcard, DOWNLOAD, TYPE_WHITE);

			// 其它
			initList(sdcard, DUOKAN, TYPE_WHITE);
			initList(sdcard, XUNLEI, TYPE_WHITE);
			initList(sdcard, IREADER, TYPE_WHITE);
			initList(sdcard, ZAPYA, TYPE_WHITE);
			initList(sdcard, KINDLE, TYPE_WHITE);
			initList(sdcard, UTORRENT, TYPE_WHITE);
			initList(sdcard, BEAM, TYPE_WHITE);

			// ===灰名单====
			initList(sdcard, GARY_QZONE_CACHE, TYPE_GARY);
			
			//===黑名单====
			initList(sdcard, BLACK_MICROMSG , TYPE_BLACK);
			initList(sdcard, BLACK_MOBILEQQ , TYPE_BLACK);			
			initList(sdcard, BLACK_MOBILEQQ1 , TYPE_BLACK);
			initList(sdcard, BLACK_AUTONAVI , TYPE_BLACK);
			initList(sdcard, BLACK_AUTONAVIDATA50 , TYPE_BLACK);
			initList(sdcard, BLACK_MIPUSH , TYPE_BLACK);
			initList(sdcard, BLACK_TENCENTNEWS , TYPE_BLACK);
			initList(sdcard, BLACK_ZHIHU , TYPE_BLACK);
			initList(sdcard, BLACK_BAIDUMAP , TYPE_BLACK);
			//ext sdcard black
			initList(EXT_SDCARD, BLACK_BAIDUMAP_EXT , TYPE_BLACK);
			initList(EXT_SDCARD, BLACK_AUTONAVIDATA50_EXT , TYPE_BLACK);
			initList(EXT_SDCARD, BLACK_ZHIHU_EXT , TYPE_BLACK);
		}
		return children;
	}
	
	private static void initList(String sdcard, String folderPath, int type)
	{
	    String realPath = adjustPath(sdcard, folderPath);
		children.put(realPath, type);
	}

	/**
	 * //修正目录名的大小写问题 sdcard fat32忽略了大小写
	 * @param sdcard
	 * @param folderPath
	 * @return
	 */
    private static String adjustPath(String sdcard, String folderPath) {        
        String realPath = "";
        File fullPath = new File(sdcard + folderPath);
        if (fullPath.exists()) {
            while (fullPath.getParentFile() != null) {
                String nowdir = fullPath.getName();
                fullPath = fullPath.getParentFile();
                if (fullPath == null) {
                    break;
                }
                String[] childlist = fullPath.list();
                if (childlist == null) {
                    break;
                }
                for (String child : childlist) {
                    if (child.equalsIgnoreCase(nowdir)) {
                        realPath = child + File.separatorChar + realPath;
                        break;
                    }
                }
            }
        }
        if (TextUtils.isEmpty(realPath))
        {
            realPath = fullPath.getPath();
        } else {  //去掉最后一个 /
            realPath = new File(realPath).getAbsolutePath();
        }
        return realPath;
    }
}
